What are Creational design patterns in Java?

by gideon.hauck , in category: Java , 2 years ago

What are Creational design patterns in Java?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by august.kutch , 2 years ago

@gideon.hauck Creational design pattern deals with the creation of objects. Decides the strategy on how the objects will be created during the software run. Since objects are basic building blocks of Object Oriented Programming. Thus to create a good piece of software the engineers must follow a proper strategy for object creation. There are a number of strategies and which one to follow depends on the software being created.


Types of Creational Design Patterns

1) Singleton Pattern: Sometimes we require that class of an object must be created only a single time throughout the application. This is done with the help of Singleton design pattern. When a class is marked as singleton then only a single instantiation of its object can exist at any point of time inside the JVM.


2) Prototype Pattern: Sometimes the object required by the software during execution is quite heavy. Allocation of memory for it and creating it from scratch may require a lot of processing power, In this case instead of creating and providing the software with a fresh copy of the object. The software searches for a copy already existing in memory and modifies it according to the need and gives the copy to the software that requests it. It saves valuable processing power of the Processor.


3) Factory Pattern: When the type of instance that has to be created depends on user input than in that case Factory pattern is advisable. This pattern will be employed when the user decides which class has to be instantiated from a number of sub-classes.


4) Abstract Factory Pattern: Abstract factory method is like a Factory pattern, the only difference is that instead of depending on the if-else block to decide the type of object to be created. We make use of inheritance principles to do the same.


5) Builder Pattern: If the class is complicated then it becomes difficult to use Abstract Factory or Factory pattern. Then it is advisable to create the object stepwise and then return it. This is the Builder pattern.

by dandre.keeling , 9 months ago

@gideon.hauck 

Creational design patterns in Java are patterns that deal with object creation mechanisms, trying to create objects in a manner suitable for a specific situation. These patterns provide various ways to create objects, while hiding the creation logic and making the code more flexible and reusable.


Some common creational design patterns in Java include:

  1. Singleton Pattern: Ensures that only one instance of a class can be created and provides a global point of access to it.
  2. Factory Method Pattern: Defines an interface for creating objects and allows subclasses to decide which class to instantiate.
  3. Abstract Factory Pattern: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  4. Builder Pattern: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  5. Prototype Pattern: Creates new objects by copying or cloning existing ones, thus avoiding the overhead of initializing objects from scratch.
  6. Object Pool Pattern: Creates a pool of reusable objects to be used by multiple clients, thus reducing object creation overhead.
  7. Lazy Initialization Pattern: Delays the creation of an object until it is actually needed, improving performance and resource utilization.


These design patterns help in achieving encapsulation, flexibility, reusability, and maintainability in Java applications by providing standard solutions to common object creation problems.